package com.capinfo.controller;

import com.alibaba.fastjson.JSONObject;
import com.capinfo.base.BaseController;
import com.capinfo.base.CurrentUser;
import com.capinfo.core.annotation.Log;
import com.capinfo.entity.CapServiceType;
import com.capinfo.entity.SysOffice;
import com.capinfo.service.CapServiceTypeService;
import com.capinfo.util.JsonUtil;
import com.capinfo.util.ReType;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author: sunbenjin
 * @date: 2020/2/29
 **/
@Controller
@RequestMapping(value="/type")
public class CapServiceTypeController extends BaseController {

    @Autowired
    private CapServiceTypeService capServiceTypeService;

    @GetMapping(value = "show")
    @RequiresPermissions("type:show")
    public String show(Model model) {
        return "/servicetype/service-type-list";
    }
    @ApiOperation(value = "/showTypeList", httpMethod = "GET", notes = "展示业务类型列表")
    @GetMapping(value = "showTypeList")
    @ResponseBody
    public ReType showType(Model model, CapServiceType capServiceType, String page, String limit) {
        if(StringUtils.isEmpty(page)){
            page = "0";
        }
        if(StringUtils.isEmpty(limit)){
            limit = "10";
        }
        return capServiceTypeService.show(capServiceType, Integer.valueOf(page), Integer.valueOf(limit));
    }
    @GetMapping(value = "addType")
    @RequiresPermissions("type:show")
    public String addType(Model model, HttpServletRequest request,CapServiceType capServiceType) {
        String pId = request.getParameter("pId");
        model.addAttribute("capServiceType",capServiceType);
        return "/servicetype/service-type-form";
    }
    @GetMapping(value = "showType")
    @RequiresPermissions("type:show")
    public String showType(Model model, HttpServletRequest request) {
       String id = request.getParameter("id");
       CapServiceType capServiceType = capServiceTypeService.selectByPrimaryKey(id);

       String detail = request.getParameter("detail");
       model.addAttribute("detail",detail);
        model.addAttribute("capServiceType",capServiceType);
        return "/servicetype/service-type-form";
    }

    @ApiOperation(value="addType",httpMethod = "POST",notes="添加或更新业务类型名称")
    @PostMapping("addType")
    @ResponseBody
    public JsonUtil addType(CapServiceType capServiceType){
        JsonUtil j = new JsonUtil();
        CurrentUser user = (CurrentUser) SecurityUtils.getSubject().getSession().getAttribute("curentUser");
        try {
            if(StringUtils.isEmpty(capServiceType.getId())){
                capServiceType.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                capServiceType.setCreateBy(user.getId());
                capServiceType.setCreateDate(new Date());
                capServiceTypeService.insertSelective(capServiceType);
            }else{
                capServiceType.setUpdateBy(user.getId());
                capServiceType.setUpdateDate(new Date());
                capServiceTypeService.updateByPrimaryKeySelective(capServiceType);
            }

            j.setMsg("保存成功");
        } catch (Exception e) {
            j.setMsg("保存失败");
            j.setFlag(false);

            e.printStackTrace();
        }
        return j;
    }




    @Log(desc = "删除", type = Log.LOG_TYPE.DEL)
    @ApiOperation(value = "/del", httpMethod = "POST", notes = "删除")
    @PostMapping(value = "/del")
    @ResponseBody
    @RequiresPermissions("dict:del")
    public JsonUtil del(String id) {
        CapServiceType capServiceType = capServiceTypeService.selectByPrimaryKey(id);
        //逻辑删除
        return capServiceTypeService.delById(id,true);
    }

    @ApiOperation(value="get",httpMethod = "GET",notes="根据id得业务类型实例")
    @GetMapping("get")
    @ResponseBody
    public JsonUtil get(String id){
        JsonUtil j = new JsonUtil();
        try {
            CapServiceType capServiceType = capServiceTypeService.selectByPrimaryKey(id);
            JSONObject jsonObject = new JSONObject();
            if(capServiceType==null){
               capServiceType = new CapServiceType();
               capServiceType.setName("");
            }
            jsonObject.put("data",capServiceType);
            j.setJsonObj(jsonObject);
            j.setMsg("加载业务类型成功");
        } catch (Exception e) {
            j.setMsg("加载业务类型失败");
            j.setFlag(false);

            e.printStackTrace();
        }
        return j;
    }
    @ApiOperation(value="loadAllServiceType",httpMethod = "GET",notes="加载所有业务类型")
    @GetMapping(value="loadAllServiceType")
    @ResponseBody
    public JsonUtil loadAllServiceType(CapServiceType capServiceType, HttpServletRequest request){
        JsonUtil j = new JsonUtil();

        try {
            List<CapServiceType> lists = capServiceTypeService.selectListByPage(capServiceType);;
            j.setFlag(Boolean.TRUE);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("data",lists);
            j.setJsonObj(jsonObject);
            System.out.println(j);
            j.setMsg("加载完成");
        } catch (Exception e) {
            j.setFlag(Boolean.FALSE);
            j.setMsg("加载异常");
            e.printStackTrace();
        }
        return j;
    }

}
